package com.sjgtw.cloud.app.controller;

import com.sjgtw.cloud.common.core.constant.CacheConstants;
import com.sjgtw.cloud.common.core.exception.BaseException;
import com.sjgtw.cloud.common.core.utils.SecurityUtils;
import com.sjgtw.cloud.common.core.web.controller.BaseController;
import com.sjgtw.cloud.common.core.web.domain.AjaxResult;
import com.sjgtw.cloud.common.core.web.page.TableDataInfo;
import com.sjgtw.cloud.common.redis.service.RedisService;
import com.sjgtw.cloud.service.SystemServerService;
import com.sjgtw.cloud.system.api.model.LoginUser;
import com.sjgtw.cloud.web.freeze.domain.FreezeFallback;
import com.sjgtw.cloud.web.freeze.domain.FreezeRecord;
import com.sjgtw.cloud.web.freeze.domain.FreezeRestore;
import com.sjgtw.cloud.web.freeze.service.IFreezeFallbackService;
import com.sjgtw.cloud.web.freeze.service.IFreezeRecordService;
import com.sjgtw.cloud.web.freeze.service.IFreezeRestoreService;
import com.sjgtw.cloud.web.freeze.vo.FreezeFallbackVO;
import com.sjgtw.cloud.web.freeze.vo.FreezeRestoreVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 冻害记录Controller
 *
 * @author lvyoufang
 * @date 2024-03-17
 */
@RestController
@RequestMapping("/appFreeze")
@Api(tags = "6.3.3、APP冻害记录")
public class AppFreezeRecordController extends BaseController {
    @Autowired
    private IFreezeRecordService freezeRecordService;

    @Autowired
    private IFreezeFallbackService freezeFallbackService;

    @Autowired
    private IFreezeRestoreService freezeRestoreService;

    @Autowired
    private SystemServerService systemServerService;

    @Autowired
    private RedisService redisService;

    /**
     * 获取冻害记录详细信息
     */
    @ApiOperation(value = "获取冻害记录详细信息")
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(freezeRecordService.selectFreezeRecordById(id));
    }

    /**
     * 新增冻害记录
     */
    @ApiOperation(value = "新增冻害记录")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody FreezeRecord freezeRecord) {
        return toAjax(freezeRecordService.insertFreezeRecord(freezeRecord));
    }

    /**
     * 修改冻害记录
     */
    @ApiOperation(value = "修改冻害记录")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody FreezeRecord freezeRecord) {
        return toAjax(freezeRecordService.updateFreezeRecord(freezeRecord));
    }

    /**
     * 删除冻害记录
     */
    @ApiOperation(value = "删除冻害记录")
    @PostMapping("/remove")
    public AjaxResult remove(String[] ids) {
        return toAjax(freezeRecordService.deleteFreezeRecordByIds(ids));
    }

    /**
     * 查询冻害记录列表
     */
    @ApiOperation(value = "查询冻害记录列表")
    @GetMapping("/list")
    public TableDataInfo list(FreezeRecord freezeRecord) {
        String token = SecurityUtils.getToken();
        String userKey = CacheConstants.LOGIN_TOKEN_KEY + token;
        LoginUser user = redisService.getCacheObject(userKey);
        if (user == null) {
            throw new BaseException("登录信息失效，请重新登录");
        }

        List<Long> deptIds = new ArrayList<>();
        if (user != null) {
            AjaxResult ajaxResult = systemServerService.getSubDeptIdsByUserId(user.getUserid());
            if (ajaxResult != null) {
                Object object = ajaxResult.get("data");
                if (object != null) {
                    deptIds = (List<Long>) object;
                }
            }
        }
        freezeRecord.setDeptIds(deptIds);

        startPage();
        List<FreezeRecord> list = freezeRecordService.selectFreezeRecordList(freezeRecord);
        return getDataTable(list);
    }

    /**
     * 查询冻害-回落情况列表
     */
    @ApiOperation(value = "查询冻害-回落情况列表")
    @GetMapping("/listFallback")
    public TableDataInfo listFallback(FreezeFallback freezeFallback) {
        startPage();
        List<FreezeFallback> list = freezeFallbackService.selectFreezeFallbackList(freezeFallback);
        return getDataTable(list);
    }

    /**
     * 查询冻害-恢复情况列表
     */
    @ApiOperation(value = "查询冻害-恢复情况列表")
    @GetMapping("/listRestore")
    public TableDataInfo listRestore(FreezeRestore freezeRestore) {
        startPage();
        List<FreezeRestore> list = freezeRestoreService.selectFreezeRestoreList(freezeRestore);
        return getDataTable(list);
    }

    /**
     * 新增冻害-回落情况
     */
    @ApiOperation(value = "新增冻害-回落情况")
    @PostMapping("/addFallback")
    public AjaxResult addFallback(@RequestBody FreezeFallbackVO fallbackVO) {
        FreezeRecord freezeRecord = freezeRecordService.selectFreezeRecordById(fallbackVO.getFreezeId());
        freezeRecord.setFallbackSubmited(fallbackVO.getSubmited());
        freezeRecordService.updateFreezeRecord(freezeRecord);
        List<FreezeFallback> fallbacks = fallbackVO.getFallbacks();
        freezeFallbackService.deleteByFreezeId(fallbackVO.getFreezeId());
        for (FreezeFallback fallback : fallbacks) {
            fallback.setFreezeId(fallbackVO.getFreezeId());
            freezeFallbackService.insertFreezeFallback(fallback);
        }
        return toAjax(fallbacks.size());
    }

    /**
     * 修改冻害-回落情况
     */
   /* @ApiOperation(value = "修改冻害-回落情况")
    @PostMapping("/editFallback")
    public AjaxResult editFallback(@RequestBody FreezeFallback freezeFallback)
    {
        return toAjax(freezeFallbackService.updateFreezeFallback(freezeFallback));
    }*/

    /**
     * 新增冻害-恢复情况
     */
    @ApiOperation(value = "新增冻害-恢复情况")
    @PostMapping("/addRestore")
    public AjaxResult addRestore(@RequestBody FreezeRestoreVO restoreVO) {
        FreezeRecord freezeRecord = freezeRecordService.selectFreezeRecordById(restoreVO.getFreezeId());
        freezeRecord.setRestoreSubmited(restoreVO.getSubmited());
        freezeRecordService.updateFreezeRecord(freezeRecord);
        List<FreezeRestore> restores = restoreVO.getRestores();
        freezeRestoreService.deleteByFreezeId(restoreVO.getFreezeId());

        String mileage = freezeRecord.getXingType();
        if (StringUtils.isNotEmpty(freezeRecord.getKilometer())) {
            String[] split = freezeRecord.getKilometer().split(",");
            if(split.length == 4){
                mileage += "K" + split[0] + "+" + split[1] + "-K" + split[2] + "+" + split[3];
            }
        }
        if (StringUtils.isNotEmpty(freezeRecord.getRemark())) {
            mileage += "(" + freezeRecord.getRemark() + ")";
        }
        for (FreezeRestore restore : restores) {
            restore.setFreezeId(restoreVO.getFreezeId());
            restore.setMileage(mileage);
            freezeRestoreService.insertFreezeRestore(restore);
        }
        return toAjax(restores.size());
    }

    /**
     * 修改冻害-恢复情况
     */
  /*  @ApiOperation(value = "修改冻害-恢复情况")
    @PostMapping("/editRestore")
    public AjaxResult editRestore(@RequestBody FreezeRestore freezeRestore)
    {
        return toAjax(freezeRestoreService.updateFreezeRestore(freezeRestore));
    }*/

}
